import { ref, reactive, toRefs } from 'vue'
import request from '@/utils/request.js'

// ======================================
// 1. 请求参数类型定义（严格 TypeScript 约束）
// ======================================
//增添列
interface CreateTableColumnsRequest {
    "databaseName": string,
    "tableName": string,
    "columnName": string,
    "dataType": string,
    "isNullable": 0|1 ; //能否为空  0不可以 1可以
    "isPrimaryKey": 0|1, //是否为主键  0否 1是
    "isForeignKey": 0|1,//是否为外键 0否 1是
    "foreignTableName": string| null,//外键关联目标表名字
    "foreignColumnName": string| null,//目标列名字
    "columnPosition": number,//列在表中的序号
    "description": string|null,//描述
    foreignKeyDescription: string|null
}
//删除
interface DeleteTableColumnsRequest {
    columnId: number|null
}
//更新列
interface UpdateTableRequest {
    columnId: number,
    columnName: string,
    dataType: string,
    isNullable: 0|1,
    isPrimaryKey: 0|1,
    isForeignKey: 0|1,
    foreignTableName: string| null,
    foreignColumnName: string| null,
    columnPosition: number,//列位置
    description: string| null,
    foreignKeyDescription: string| null,

}
// ======================================
// 2. 响应结果类型（泛型增强 + 语义化）
// ======================================
interface BaseResponse<T = any> {
    code?: number
    message?: string
    data: {
        data: T;  // 增加嵌套层级
        timestamp: number;
    };
}
// ======================================
// 3. 通用请求工具（消除重复逻辑）
// ======================================
const baseRequest = async <T>(
    state: {
        loading: boolean
        error: string | null
        response: BaseResponse<T> | null
    },
    url: string,
    method: 'GET' | 'POST'|'DELETE',
    params?: Record<string, any>, // GET 参数
    data?: Record<string, any>    // POST 请求体
): Promise<BaseResponse<T>> => {
    state.loading = true
    state.error = null
    state.response = null

    try {
        const response = await request({ url, method, params, data })
        state.response = response as BaseResponse<T>
        return response
    } catch (error: any) {
        state.error = error.message || '请求失败'
        throw error // 抛出错误供上层处理
    } finally {
        state.loading = false
    }
}


// ======================================
// 4. 业务方法 & 状态管理（清晰隔离）
// ======================================
export const useTableColumnsService = () => {
    // 每个方法独立管理状态，避免混淆
    const createColumnsState = reactive({
        loading: false,
        error: null as string | null,
        response: null as BaseResponse<unknown> | null,
    })

    const deleteColumnsState = reactive({
        loading: false,
        error: null as string | null,
        response: null as BaseResponse<unknown> | null,
    })

    const updateTableColumnsState = reactive({
        loading: false,
        error: null as string | null,
        response: null as BaseResponse<unknown> | null,
    })

    // -------------------- 创建数据表 --------------------
    const createTableColumns= async (params: CreateTableColumnsRequest) => {
        return baseRequest(createColumnsState, '/database/createColumn', 'POST', undefined, params)
    }

    // -------------------- 删除数据列 --------------------
    const deleteColumn= async (params: DeleteTableColumnsRequest) => {
        return baseRequest(deleteColumnsState, '/database/deleteColumn', 'DELETE', params)
    }
    // -------------------- 更新数据列 --------------------
    const updateTableColumn = async (params: UpdateTableRequest) => {
        return baseRequest(updateTableColumnsState, '/database/updateColumn', 'POST', undefined, params)
    }



    // ======================================
    // 5. 导出响应式状态 & 方法
    // ======================================
    return {
        ...toRefs(createColumnsState),
        ...toRefs(deleteColumnsState),
        ...toRefs(updateTableColumnsState),
        createTableColumns,
        updateTableColumn,
        deleteColumn
    }
}